//
//  WH_MeViewController.m
//  App_6.23
//
//  Created by 芦珊 on 16/6/24.
//  Copyright © 2016年 lushan. All rights reserved.
//

#import "WH_MeViewController.h"
#import "LS_HeaderView.h"
#import "LS_RechargeViewController.h"
#import "WH_LoginView.h"
#import "WH_Home.h"
#import "WSRegistViewController.h"
#import "WH_MyinfoViewController.h"
#import "WH_AllOrderViewController.h"
#import "WH_LuckViewController.h"
#import "WH_RechargeViewController.h"
#import "WH_ForgerViewController.h"
#import "WH_ShareListViewController.h"

@interface WH_MeViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate,LoginViewDelegate,LS_HeaderViewDelegate>

@property (nonatomic , strong)UITableView *myTableView;
@property (nonatomic , strong)LS_HeaderView *headV;

@property (nonatomic , strong)UITableViewCell *cell;

@property (nonatomic , strong)UIButton *rechargeBtn; //充值按钮
@property (nonatomic , strong)UIButton *underwayBtn;  //正在进行
@property (nonatomic , strong)UIButton *announcedBtn;  //已经揭晓
@property (nonatomic , strong)UIButton *multiphaseBtn;  //多期参与

@property (nonatomic, copy) NSString *userHeard;//账户头像url

@end

@implementation WH_MeViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self postData];
    self.navigationController.navigationBar.hidden = YES;

}

-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"个人中心";

    [self createMyTable];
    
    if (![[[NSUserDefaults standardUserDefaults] objectForKey:@"Login"] isEqualToString:@"1"]) {
        [self createLoginButton];
    }
    
}

- (void)createLoginButton{
    
    WH_LoginView *loginV = [[WH_LoginView alloc] initWithFrame:self.view.bounds];
    loginV.delegate = self;
    loginV.tag = 559;
    [self.view addSubview:loginV];
}
- (void)loginWithUserName:(NSString *)userName passWord:(NSString *)passWord {
    
    
    NSString *body = [NSString stringWithFormat:@"action=user_login&user_name=%@&pwd=%@",userName,passWord];
    [WH_AppTools postUrl:URLstr body:body result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        //NSLog(@"result==%@",result);
        NSDictionary *dic = result;
        NSString *status = [dic objectForKey:@"status"];
        NSString *message = [dic objectForKey:@"message"];
        NSDictionary *user_array = [dic objectForKey:@"user_array"];
        WH_Home *user = [WH_Home modelWithDictionary:user_array];
        if ([status isEqualToString:@"success"]) {
            
            [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"Login"];
            [[NSUserDefaults standardUserDefaults] setObject:user.user_id forKey:USERID];
            [[NSUserDefaults standardUserDefaults] setObject:userName forKey:@"userName"];
            [MobClick event:@"Login"];
            [MobClick profileSignInWithPUID:NSUSERID];
            [self dismissLogin];
            [self postData];
        }else{
            
            [WSProgressHUD showImage:nil status:message];
        }
        
        [self.myTableView reloadData];

    } failure:^(NSError *error) {
        
        
    }];
    
}
-(void)dismissLogin{
    
    UIView *vLogin = [self.view viewWithTag:559];
    vLogin.alpha = 0;
    [vLogin removeFromSuperview];
}
#pragma mark - 忘记密码

- (void)pushForgetViewController{
    WH_ForgerViewController *forgetVC = [[WH_ForgerViewController alloc] init];
    
    [self.navigationController pushViewController:forgetVC animated:YES];
    
}
#pragma mark - 注册

- (void)pushRegistViewController {
    
    WSRegistViewController *registVC = [[WSRegistViewController alloc] init];
    [self.navigationController pushViewController:registVC animated:YES];

}

- (void)createMyTable{
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49) style:UITableViewStyleGrouped];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"MeCell"];
    [self.view addSubview:self.myTableView];
    [self createHeadView];
    [self postData];
}

#pragma mark -头视图
- (void)createHeadView{
    
    self.headV = [[LS_HeaderView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, SCREEN_WIDTH, SCREEN_HEIGHT / 4 + 40)];
    [self.headV createHeadView:@"" price:@"0" nick:@""];
    self.myTableView.tableHeaderView = self.headV;
    self.headV.delegate = self;
    
}
#pragma mark - postData

-(void)postData{
    
    
    NSString *body = [NSString stringWithFormat:@"action=account&user_id=%@",NSUSERID];
    [WH_AppTools postUrl:URLstr body:body result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        
        // NSLog(@"%@",result);
        NSDictionary *dic = result;
        NSDictionary *user_array = [dic objectForKey:@"user_array"];
        WH_Home *mine = [WH_Home modelWithDictionary:user_array];
        [self.headV createHeadView:mine.user_nick price:mine.user_price nick:mine.user_avatar];
        _userHeard = mine.user_avatar;
        [self.myTableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
    
    
}
#pragma mark - HeardView  Delegate

-(void)pushtoRechargVC{
    
    
    WH_RechargeViewController *rechargeVC = [[WH_RechargeViewController alloc] init];
    [self.navigationController pushViewController:rechargeVC animated:YES];
}

-(void)pushToView{
    
    WH_MyinfoViewController *myVC = [[WH_MyinfoViewController alloc] init];
    myVC.userAvatar = self.userHeard;
    [self.navigationController pushViewController:myVC animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    } else if (section == 1){
        return 1;
    } else if (section == 2){
        return 1;
    } else if (section == 3){
        return 1;
    }
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellId = @"MeCell";
    self.cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    _cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (indexPath.section == 0 && indexPath.row == 0) {

        self.cell.textLabel.text = @"全部夺宝记录";
        self.cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else if (indexPath.section == 0 && indexPath.row == 1){
        self.cell.textLabel.text = @"幸运记录";
        self.cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


    }else if (indexPath.section == 1 && indexPath.row == 0){

        self.cell.textLabel.text = @"清除缓存";
        self.cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        
    }else if (indexPath.section == 2 && indexPath.row == 0){
        
        self.cell.textLabel.text = @"晒单";
        self.cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }else if (indexPath.section == 3 && indexPath.row == 0){
        self.cell.selectionStyle = UITableViewCellSeparatorStyleNone;

        self.cell.textLabel.text = @"退出登录";
        self.cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    else{
        
        self.cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    
    return _cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    //if (section == 0) {
        //return 0.01;//这里是设置tableView的第一部分的头视图高度为0.01
    //}else {
        return 10;//这里设置其他部分的头视图高度为10
    //}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;//设置尾视图高度为0.01
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return SCREEN_HEIGHT / 13;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 && indexPath.row == 0){
        
        WH_AllOrderViewController *allOdert = [[WH_AllOrderViewController alloc] init];
        
        [self.navigationController pushViewController:allOdert animated:YES];
    }else if (indexPath.section == 0 && indexPath.row == 1){
        
        WH_LuckViewController *luckVC = [[WH_LuckViewController alloc] init];
        
        [self.navigationController pushViewController:luckVC animated:YES];
        
    }else if (indexPath.section == 1 && indexPath.row == 0){
        
        NSUInteger size = [[SDImageCache sharedImageCache] getSize];
        float cacheSize = size / 1024.0 / 1024.0;
        NSString *string = [NSString stringWithFormat:@"缓存大小为%.2fMB", cacheSize];
        [self alertViewControllerWithTitle:@"清除缓存" content:string block:^{
            
            // 清除缓存
            [[SDImageCache sharedImageCache] clearDisk];
        }];
        
    }else if(indexPath.section == 3 && indexPath.row == 0){
        
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"Login"];
        [MobClick profileSignOff];
        FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
        [loginManager logOut];
        [self createLoginButton];
    }else if (indexPath.section == 2 && indexPath.row == 0){
        
        WH_ShareListViewController *shareListVC = [[WH_ShareListViewController alloc] init];
        
        [self.navigationController pushViewController:shareListVC animated:YES];
    }

    
}

#pragma mark -三个按钮
- (void)createThreeButton{
    //正在进行按钮
    self.underwayBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.underwayBtn.backgroundColor = ARCDOM_4;
    [self.underwayBtn setTitle:@"正在进行" forState:UIControlStateNormal];
    [self.underwayBtn addTarget:self action:@selector(underwayAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell addSubview:_underwayBtn];
    [self.underwayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@10);
        make.left.equalTo(@0);
        make.bottom.equalTo(@-10);
        make.width.equalTo(@(SCREEN_WIDTH / 3));
    }];
    
    
    //已经揭晓按钮
    self.announcedBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.announcedBtn.backgroundColor = ARCDOM_4;
    [self.announcedBtn setTitle:@"已经揭晓" forState:UIControlStateNormal];
    [self.announcedBtn addTarget:self action:@selector(announcedAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell addSubview:_announcedBtn];
    [self.announcedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@10);
        make.left.equalTo(self.underwayBtn.mas_right);
        make.bottom.equalTo(@-10);
        make.width.equalTo(@(SCREEN_WIDTH / 3));
    }];
    
    
    
    //多期参与按钮
    self.multiphaseBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.multiphaseBtn.backgroundColor = ARCDOM_4;
    [self.multiphaseBtn setTitle:@"多期参与" forState:UIControlStateNormal];
    [self.multiphaseBtn addTarget:self action:@selector(multiphaseAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell addSubview:_multiphaseBtn];
    [self.multiphaseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@10);
        make.left.equalTo(self.announcedBtn.mas_right);
        make.bottom.equalTo(@-10);
        make.width.equalTo(@(SCREEN_WIDTH / 3));
    }];
    
    
}

#pragma mark -正在进行响应方法
- (void)underwayAction:(UIButton *)btn{
    NSLog(@"正在进行");
}
#pragma mark -已经揭晓响应方法
- (void)announcedAction:(UIButton *)btn{
    NSLog(@"已经揭晓");
}
#pragma mark -多期参与响应方法
- (void)multiphaseAction:(UIButton *)btn{
    NSLog(@"多期参与");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
